package com.selenium;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @program: selenium
 * @description: test
 * @author: abel.he
 * @create: 2021-04-01 20:38
 **/
public class SeleniumTest {

    // https://sites.google.com/a/chromium.org/chromedriver/downloads
    // 需要下载本地chrome 对应版本的驱动程序

    public static void main(String[] args) {
        WebDriver driver = null;

        ChromeOptions options = new ChromeOptions();
        File chrome = new File("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe");
        System.out.println(chrome.getAbsolutePath());
        options.setBinary(chrome.getAbsolutePath());

        // 隐藏chrome爬数据
        options.addArguments("--headless");

        File file = new File("D:\\driver\\chromedriver.exe");
        System.out.println(file.getAbsolutePath());
        System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
        try {
            driver = new ChromeDriver(options);
//            driver.get("https://article.xuexi.cn/articles/index.html?art_id=3235027273028715854&item_id=3235027273028715854&study_style_id=feeds_default");
            driver.get("https://article.xuexi.cn/articles/index.html?art_id=1037189282035060032&study_style_id=video_default");

            driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);

//            Thread.sleep(5000);
            //获取JS执行器，可以执行js代码来控制页面
            //            String source = driver.getPageSource();
            String source = pageScroll(driver);

            String total = driver.getTitle().replaceAll(" \\s\\|\\*\\?\"':<>丨","").replace("|", "");
            System.out.println(total);
            System.out.println(total.replaceAll(" \\s\\|\\*\\?\"':<>丨",""));

            saveFile(String.join("", total, ".html"), source);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("close");
            if (null != driver) {
                driver.close();
            }
        }
    }

    /**
     * js操作
     * @param driver
     * @return
     */
    private static String pageScroll(WebDriver driver) {

        JavascriptExecutor driver_js= ((JavascriptExecutor) driver);
        Long height = (Long) driver_js.executeScript("return document.body.scrollHeight");
        long scrollNumber = height / 100;
        scrollNumber = height % 100 !=0 ? ++ scrollNumber : scrollNumber;
        for (int i = 0; i < scrollNumber; i++){
            int start = i + 100;
            int end = (i+1) * 100;
            driver_js.executeScript("scroll("+start+","+end+")");
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        // js 点击事件
        driver_js.executeScript("document.getElementById(\"edit-info-action\").click()");
        // 移除dom元素
        driver_js.executeScript("document.getElementsByClassName(\"diplayer-loading-icon\")[0].remove();");
        // 显示video控件事件
        driver_js.executeScript("document.getElementsByClassName(\"video dplayer-video dplayer-video-current\")[0].controls = true;");


//        try {
//            Thread.sleep(15000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        driver_js.executeScript("document.getElementsByClassName(\"video dplayer-video dplayer-video-current\")[0].play();");
//        try {
//            Thread.sleep(15000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        driver_js.executeScript("document.getElementsByClassName(\"video dplayer-video dplayer-video-current\")[0].pause();");
        return (String)driver_js.executeScript("return document.documentElement.outerHTML");
    }

    public static File saveFile(String fileName,String content) throws IOException {
        File out = new File("G:\\output\\"+fileName);
        FileUtils.writeStringToFile(out,content,"utf-8");
        return out;
    }

}
